home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 1162 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. From: saa@ecofin.ch (Angelo Salvade)
  2. Message-ID: <00614.2912664663.294@ecofin.uucp>
  3. X-Original-Date: Thu, 18 Apr 1996 9:30:03 -0100
  4. Path: in1.uu.net!bounce-back
  5. Date: 19 Apr 96 14:43:42 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Newsgroups: comp.std.c++
  9. X-Umcp-Priority: 1
  10. Subject: Are these "implicit" casts legal?
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMXem6eEDnX0m9pzZAQGSFwF/UuBIB5ms4HHVdDsyxxcILLKlyRuyK4U4
  13.     QrSkrapo/zvZkTbnD0YFmFOfBVHZX4ry
  14.     =eg4z
  15.  
  16. Can anybody tell me if the following code is legal:
  17.  
  18. //----------------------------------------------------------------
  19.  
  20. class A {};
  21.  
  22. class B: public A {};
  23.  
  24. //----------------------------------------------------------------
  25.  
  26. // with templates
  27.  
  28. template <class O>
  29. class Ref {
  30.     public:
  31.         Ref();
  32.         Ref(O* value);
  33.         operator O*();
  34. };
  35.  
  36. extern void f1(Ref<A> a);
  37.  
  38. static void t1()
  39. {
  40.     Ref<B> b;
  41.     Ref<A> a(b); // #1
  42.  
  43.     f1(b); // #2
  44. }
  45.  
  46. //----------------------------------------------------------------
  47.  
  48. // without templates
  49.  
  50. class RefA {
  51.     public:
  52.         RefA(A* value);
  53. };
  54.  
  55. class RefB {
  56.     public:
  57.         RefB();
  58.         operator B*();
  59. };
  60.  
  61. extern void f2(RefA a);
  62.  
  63. static void t2()
  64. {
  65.     RefB b;
  66.     RefA a(b); // #3
  67.  
  68.     f2(b); // #4
  69. }
  70.  
  71. //----------------------------------------------------------------
  72.  
  73. I believe that #1-4 should work identically and are correct C++:
  74. A Ref<A> should be initialised with a Ref<B>.
  75. The following steps should happen:
  76. - call of Ref<B>.operator B*()
  77. - C++ built in conversion of B* to A*
  78. - call of constructor Ref<A>(A*)
  79.  
  80. I checked this code with the Metrowerks CW8,
  81. GNU 2.7.2 and SunPro C++ 4.0.1 & C++ 4.1 compilers.  
  82. The SunPro C++ 4.1 is the only compiler of those 
  83. that didn't compile #2 and #4. It gave the following message:
  84. "Error: Cannot use Ref<B> to initialize Ref<A>".
  85.  
  86. Is my code illegal or is this a bug in the compiler?
  87.  
  88. Angelo Salvade
  89. salvade@ecofin.ch
  90. ---
  91. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  92. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  93. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  94. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  95. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  96.